#!/bin/sh

export E2E_TIMEOUT="${E2E_TIMEOUT:-180}"

wait_until() {
  notrace_function traceable_wait_until "$@"
}

traceable_wait_until() {
  local RESULT
  local EXIT_CODE
  local START="$(date +%s)"
  local TIMEOUT="$E2E_TIMEOUT"
  local INTERVAL=2
  local ID="$(shuf -i 0-65535 -n 1)"
  local SUFFIX="$(printf '%s' "$*" | head -c 192 | tr -c '[A-Z][a-z][0-9]_-' ' ' | tr -s ' ')"
  local OUTPUT_PATH="$LOG_PATH/wait-until-$START-$ID-$SUFFIX.log"
  local LAST_OUTPUT_PATH="$LOG_PATH/wait-until-$START-$ID-$SUFFIX-last.log"
  local LAST_ERROR_PATH="$LOG_PATH/wait-until-$START-$ID-$SUFFIX-last.err.log"
  while echo_raw "$1" | grep -q '^-'
  do
    if [ "$1" = "-t" ]
    then
      shift
      TIMEOUT="$1"
      shift
    fi
    if [ "$1" = "-i" ]
    then
      shift
      INTERVAL="$1"
      shift
    fi
  done
  echo -n >"$OUTPUT_PATH"
  while [ "$((START + TIMEOUT))" -gt "$(date +%s)" ]
  do
    try_function "$@" > "$LAST_OUTPUT_PATH" 2> "$LAST_ERROR_PATH"
    cat "$LAST_OUTPUT_PATH" "$LAST_ERROR_PATH" >> "$OUTPUT_PATH"
    if "$RESULT"
    then
      cat "$LAST_OUTPUT_PATH"
      return
    fi
    sleep "$INTERVAL"
  done
  cat "$OUTPUT_PATH"
  return 1
}
